from typing import List, Tuple
import shapefile as shp


def path_to_shp(path_points: List[Tuple[float, float]], shp_path: str):
    """将pp的路径结果集转为可视化的shp文件"""

    # 转换路径点格式为 (lon, lat)
    path_points = [(lon, lat) for lat, lon in path_points]

    # 创建一个新的SHP文件，指定类型为 PolyLine (3)
    with shp.Writer(shp_path, shapeType=shp.POLYLINE) as writer:
        writer.field('id', 'N')

        # 写入路径线
        writer.line([path_points])
        writer.record(1)  # 确保记录数量与几何形状数量一致

    # 创建对应的PRJ文件，定义WGS84坐标系
    prj_path = shp_path.replace('.shp', '.prj')
    with open(prj_path, 'w') as prj_file:
        prj_file.write("""GEOGCS["WGS 84",
            DATUM["WGS_1984",
                SPHEROID["WGS 84",6378137,298.257223563,
                    AUTHORITY["EPSG","7030"]],
                AUTHORITY["EPSG","6326"]],
            PRIMEM["Greenwich",0,
                AUTHORITY["EPSG","8901"]],
            UNIT["degree",0.0174532925199433,
                AUTHORITY["EPSG","9122"]],
            AUTHORITY["EPSG","4326"]]""")


def psth_to_json(path_points: List[Tuple[float, float]], json_path: str):
    """将pp的路径结果集转为导航json文件"""